package com.zzyl.nursing.job;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzyl.nursing.domain.Contract;
import com.zzyl.nursing.service.IContractService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;


@Component("updateContractStatusJob")
public class UpdateContractStatusJob {

     @Autowired
    private IContractService contractService;

    public void updateStatus() {

        //未生效改成已生效
        //SQL:update contract set status = 1 where status=0  and  end_date >now() and start_date<now();
        LambdaUpdateWrapper<Contract> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Contract::getStatus, 1);
        wrapper.eq(Contract::getStatus, 0);
        //开始时间小于现在时间
        wrapper.lt(Contract::getStartDate, LocalDateTime.now());
        //结束时间大于现在时间
        wrapper.gt(Contract::getEndDate, LocalDateTime.now());
        contractService.update(wrapper);


        //已生效--已过期
        //SQL:update contract set status = 2 where status=1 and  end_date < now();
        LambdaUpdateWrapper<Contract> wrapper1 = new LambdaUpdateWrapper<>();
        wrapper1.set(Contract::getStatus, 2);
        wrapper1.eq(Contract::getStatus, 1);
        //结束时间小于现在时间
        wrapper1.lt(Contract::getEndDate, LocalDateTime.now());
        contractService.update(wrapper1);

    }
}
